package com.yy.service;

import com.yy.clientEntities.TestData;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Author young
 * Date 2023/1/3 22:57
 * Description: 模拟对数据库的基本操作
 */
@Service
public class TestDataService {
    private static Map<Integer, TestData> col=null;
    private Integer initId=7;
    static {
        col=new HashMap<>();
        col.put(1,new TestData(1,"水浒传",100));
        col.put(2,new TestData(2,"西游记",180));
        col.put(3,new TestData(3,"三国演义",60));
        col.put(4,new TestData(4,"红楼梦",75));
        col.put(5,new TestData(5,"造梦西游",66));
        col.put(6,new TestData(6,"大话西游",97));
    }
    
    //增
    public String add(TestData testData){
        if (testData.getId()==null){
            testData.setId(initId++);
        }
        col.put(testData.getId(),testData);
        return "数据添加成功";
    }
    //删
    public String delete(Integer id){
        col.remove(id);
        return "数据删除成功";
    }
    //改
    public TestData update(TestData testData){
        Assert.notNull(testData,"数据为空");
        return col.replace(testData.getId(), testData);
    }
    //根据id查
    public TestData findOne(Integer id){
        return col.get(id);
    }
    //查全部
    public Collection<TestData> findAll(){
       return col.values();
    }
}
